﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pharmacy.Data;
using System.Data.SqlClient;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;
using Pharmacy.Entities;

public partial class Admin_PreviewCost : System.Web.UI.Page
{
    DateTime tmpfromdate, tmptodate, fromdate, todate;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString["From"] != null && Request.QueryString["To"] != null)
                {
                    tmpfromdate = DateTime.Parse(Request.QueryString["From"].ToString());
                    tmptodate = DateTime.Parse(Request.QueryString["To"].ToString());
                    fromdate = new DateTime(tmpfromdate.Year, tmpfromdate.Month, tmpfromdate.Day, 0, 0, 0);
                    todate = new DateTime(tmptodate.Year, tmptodate.Month, tmptodate.Day, 23, 59, 59);
                    PreviewCost(fromdate, todate);

                }
            }
            catch (Exception)
            {

            }
        }
    }
    private void PreviewCost(DateTime from,DateTime to)
    {
        
       
        //DateTime fromdate = DateTime.Parse("01/01/2011");
        //DateTime todate = DateTime.Parse("6/11/2011");
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "_Grn_GetByDate";
        cmd.Parameters.Add("@fromdate", SqlDbType.DateTime).Value = from;
        cmd.Parameters.Add("@todate", SqlDbType.DateTime).Value = to;
        ds = DataRepository.Provider.ExecuteDataSet(cmd);
        DataTable ntableGrn = ds.Tables[0];
        DataTable ntableIA = ds.Tables[1];

        

        decimal totalGrn = 0;
        decimal amountGrn = 0;
        decimal amountIA = 0;
        decimal totalIA = 0;
        DSCost.GrnDataTable dtGrn = new DSCost.GrnDataTable();
        DSCost.GrnRow dtGrnRow;
        for (int i = 0; i < ntableGrn.Rows.Count; i++)
        {
            dtGrnRow = dtGrn.NewGrnRow();
            DataRow drw = ntableGrn.Rows[i];
            dtGrnRow.GrnId=drw["GrnId"].ToString();
            dtGrnRow.Grndate =DateTime.Now;
            dtGrnRow.SupplierID = drw["SupplierID"].ToString();
            dtGrnRow.PrepareBy = drw["Name"].ToString();
            dtGrnRow.Remark = drw["Remark"].ToString();
            dtGrnRow.Amount =decimal.Parse( drw["Amount"].ToString());
            dtGrn.AddGrnRow(dtGrnRow);
            amountGrn = decimal.Parse(drw["Amount"].ToString());
            totalGrn += amountGrn;
        }

        DSCost.ImportAdjustDataTable dtIA = new DSCost.ImportAdjustDataTable();
        DSCost.ImportAdjustRow dtIARow;
        for (int i = 0; i < ntableIA.Rows.Count; i++)
        {
            dtIARow = dtIA.NewImportAdjustRow();
            DataRow drw = ntableIA.Rows[i];
            dtIARow.GrnId = drw["GrnId"].ToString();
            dtIARow.Grndate = DateTime.Now;
            dtIARow.SupplierID = drw["SupplierID"].ToString();
            dtIARow.PrepareBy = drw["Name"].ToString();
            dtIARow.Remark = drw["Remark"].ToString();
            dtIARow.Amount = decimal.Parse(drw["Amount"].ToString());
            dtIA.AddImportAdjustRow(dtIARow);
            amountIA = decimal.Parse(drw["Amount"].ToString());
            totalIA += amountIA;
        }
        DSCost.TitleDataTable dtTitle = new DSCost.TitleDataTable();
        DSCost.TitleRow dtTitleRow = dtTitle.NewTitleRow();
        dtTitleRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtTitleRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;
        dtTitleRow.TotalAmountGrn =totalGrn;
        dtTitleRow.TotalAmountIA = totalIA;
        //get prepareby from cookies
        var httpCookie = Request.Cookies["LoginUser"];
        WebUser id = DataRepository.WebUserProvider.GetByUserId(httpCookie.Value);
        dtTitleRow.PrepareBy = id.Name;
        dtTitle.AddTitleRow(dtTitleRow);

        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSCost_Grn", dtGrn));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSCost_Title", dtTitle));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSCost_ImportAdjust", dtIA));
        ReportViewer1.DataBind();
    }
}
